<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * +----------------------------------------------------------------------
 * 用户管理
 * +----------------------------------------------------------------------
 * Author: ZF <ZFajpst_2008@126.com> 2012-10-23 16:55:00
 * +----------------------------------------------------------------------
 */
	
	class User extends MY_Controller {
		
		public function __construct(){
			parent::__construct();
			$this->load->model('user_model', 'm');
			$this->load->model('depart_model', 'm1');
		}
		
		/**
		 * +---------------------
		 * 用户管理
		 * +---------------------
		 */
		public function index($id = 0){
			$this->load->library('pagination');
			
			$config['base_url'] = '/index.php/user/index/' . $id . '/';
			$config['uri_segment'] = 4;
			$config['total_rows'] = $this->m->getcount($id);
			$config['per_page'] = 20;
			$config['num_links'] = 2;
			$config['first_link'] = false;
			$config['last_link'] = false;
			$config['next_link'] = '下一页';
			$config['prev_link'] = '上一页';
			$config['prev_tag_open'] = '<span class="go_up">';
			$config['prev_tag_close'] = '</span>';
			$config['next_tag_open'] = '<span class="go_down">';
			$config['next_tag_close'] = '</span>';
			$config['cur_tag_open'] = '<em>';
			$config['cur_tag_close'] = '</em>';
			$config['num_tag_open'] = '';
			$config['num_tag_close'] = '';
			$config['use_page_numbers'] = true;

			$this->pagination->initialize($config);
		
			$data['id'] = $id;
			$data['list'] = $this->m->index($id, $config['per_page'], $this->uri->segment(4));
			$data['page'] = $this->pagination->create_links();
			$data['pagenum'] = $this->uri->segment(4);
			$data['menu'] = $this->getMenu();
			// $data['menu'] = array('menu' => $this->getMenu());
			
			$this->load->view('user_index', $data);
		}
		
		/**
		 * +---------------------
		 * 用户添加
		 * +---------------------
		 */
		public function add($action = ''){
			// 加载辅助类、函数
			$this->load->helper('form');
			$this->load->library('form_validation');
			
			// 初始化数据
			$data['list'] = $this->m1->getDepart();
			$data['menu'] = $this->getMenu();
			
			// 显示、执行添加
			if($action != 'do'){
				$this->load->view('user_add', $data);
			}else{
				$this->form_validation->set_rules('departid', '所属部门', 'required');
				$this->form_validation->set_rules('username', '用户名', 'required');
				$this->form_validation->set_rules('userpwd', '密码', 'required');
				
				if($this->form_validation->run() == false){
					$this->load->view('user_add', $data);
				}else{
					if($this->m->add()){
						echo '<script>alert("数据添加成功！");window.location = "/index.php/user"</script>';
					}else{
						echo '<script>alert("数据添加失败！");window.history.back()</script>';
					}
				}
			}
		}
		
		/**
		 * +---------------------
		 * 用户修改
		 * +---------------------
		 */
		public function edit($id, $action = ''){
			// 加载辅助类、函数
			$this->load->helper('form');
			$this->load->library('form_validation');
			
			// 初始化数据
			$data['id'] = $id;
			$data['list'] = $this->m1->getDepart();
			$data['info'] = $this->m->edit($id);
			$data['menu'] = $this->getMenu();
			
			// 显示、执行修改
			if($action != 'do'){
				$this->load->view('user_edit', $data);
			}else{
				$this->form_validation->set_rules('departid', '所属部门', 'required');
				$this->form_validation->set_rules('username', '用户名', 'required');
				
				if($this->form_validation->run() == false){
					$this->load->view('user_edit', $data);
				}else{
					if($this->m->edit($id, 'do')){
						echo '<script>alert("数据修改成功！");window.location = "/index.php/user"</script>';
					}else{
						echo '<script>alert("数据修改失败！");window.history.back()</script>';
					}
				}
			}
		}
		
		/**
		 * +---------------------
		 * 用户删除
		 * +---------------------
		 */
		public function del($id = 0){
			if($this->m->del($id)){
				echo '<script>alert("数据删除成功！");window.location = "/index.php/user"</script>';
			}else{
				echo '<script>alert("数据删除失败！");window.history.back()</script>';
			}
		}
		
	}